package bookStore.eBook.android;

import bookStore.eBook.android.book6DEC5843781E4012ABB63C151354C73B.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EBookEntryActivity extends Activity {
	
	public static final String TAG = "eBook:Bright";
	
	private DisplayMetrics dm;
	private ListView mListView;
	private TextView mTextView;
	private ChapterManager mChapterManager;
	//private List<String> mChapters = new ArrayList<String>();
	//private HashMap<String,String> mChapter2ID = new HashMap<String,String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		

		setContentView(R.layout.entry);
		
		Button buttonContinue = (Button)findViewById(R.id.buttonContinueRead);
		//View v = findViewById(R.id.layoutEntry);
		//v.setBackgroundResource(R.drawable.shelf_bkg);
		
		mListView =  (ListView)findViewById(R.id.listViewContent);
		mTextView =  (TextView)findViewById(R.id.textViewBookName);

		try {
			mChapterManager = ChapterManager.CreateChapterManager(this, R.raw.index);
			
		} catch (Exception e1) {
			e1.printStackTrace();
			Toast.makeText(this, "Cannot find the content!",
					Toast.LENGTH_LONG).show();
			this.finish();
		}
		
		// set the book name title
		mTextView.setText(mChapterManager.getBookName());
		
		final ColorStateList csl = this.getResources().getColorStateList(R.drawable.text_color);
		//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,mChapters);
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(
	            this,android.R.layout.simple_list_item_1, mChapterManager.getAllChapters()){
	        @Override
	        public View getView(int position, View convertView, android.view.ViewGroup parent) {
	            View view =super.getView(position, convertView, parent);
	            view.setBackgroundColor(0xffffffff);
	            TextView textView=(TextView) view.findViewById(android.R.id.text1);
	            
	            textView.setBackgroundResource(R.drawable.text_bg_color);
	            
	            textView.setTextColor(csl);
	            return view;
	        }
	    };
		
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Log.e(TAG, "The item is clicked: " + arg2 + " and " + arg3);
				String chapterName = mChapterManager.getChapterName((int)arg3);
				int chapterID = mChapterManager.getChapterResourceID(chapterName);

				Intent intent = new Intent();
				intent.setClass(EBookEntryActivity.this, ReadingActivity.class);
				
		        // pass charpter and page number to ReadingActivity        
		        Bundle bundle=new Bundle();
		        bundle.putString("ChapterName",chapterName);
		        bundle.putInt("ChapterID",chapterID);
		        intent.putExtras(bundle);
		           
				startActivityForResult(intent, 0);
			}
			
		});
		
		buttonContinue.setOnClickListener(new OnClickListener(){
			
			public void onClick(View v){
				ChapterManager cm = ChapterManager.getChapterManager();
				if(cm== null)
				{
					return;
				}
				
				Intent intent = new Intent();
				intent.setClass(EBookEntryActivity.this, ReadingActivity.class);
				
		          // pass charpter and page number to ReadingActivity        
		           Bundle bundle=new Bundle();
		           bundle.putBoolean("isContinue", true);
		           bundle.putString("ChapterName",cm.getChapterName(0));
			       bundle.putInt("ChapterID",cm.getChapterResourceID(0));
		           intent.putExtras(bundle);
		           
				startActivityForResult(intent, 0);
			 }

		});


	}
}